grdfilter

Filter a grid in the space (or time) domain

Synopsis

G = grdfilter(cmd0::String="", arg1=nothing; kwargs...)

Description

grdfilter filters a grid in the time (or space) domain using one of the selected convolution or non-convolution isotropic or rectangular filters and computes distances using Cartesian or Spherical geometries. The output grid can optionally be generated as a sub-region of the input (via region/R) and/or with new increment (via inc/I) or registration (via toggle/T). In this way, one may have “extra space” in the input data so that the edges will not be used and the output can be within one half-width of the input edges. If the filter is low-pass, then the output may be less frequently sampled than the input.

Note: For filtering in the frequency (or wavenumber) domain instead, see grdfft.

Required Arguments

Input Grid

  • First positional argument or ingrid: Input grid as a file name (String) or a GMTgrid object
    • Optionally, append =ID for reading a specific file format [Default is =nf]
    • Append ?varname for a specific netCDF variable
    • Modifiers supported:
      • +b: Select a band [Default is 0]
      • +d: Divide data values by given divisor [Default is 1]
      • +n: Replace data values matching invalid with NaN
      • +o: Offset data values by given offset [Default is 0]
      • +s: Scale data values by given scale [Default is 1]

distance or D

Distance flag tells how grid (x, y) relates to filter width. Required option.

Syntax: D=flag or distance=flag where flag is one of: - D="p" or D=:p: Grid (px,py) with width an odd number of pixels; Cartesian distances - D=0 or D="0": Grid (x,y) same units as width, Cartesian distances - D=1 or D="1": Grid (x,y) in degrees, width in km, Cartesian distances - D=2 or D="2": Grid (x,y) in degrees, width in km, dx scaled by cos(middle y), Cartesian distances - D=3 or D="3": Grid (x,y) in degrees, width in km, dx scaled by cos(y), Cartesian distances (slower) - D=4 or D="4": Grid (x,y) in degrees, width in km, Spherical distance calculation (slower) - D=5 or D="5": Grid (x,y) in Mercator -Jm1 img units, width in km, Spherical distance calculation

Notes: - Flags 0-2 are fastest (weight matrix computed once) - Flags 3-5 are slower (weights recomputed for each latitude)

Examples:

# Spherical distance calculation for geographic grid
G = grdfilter("@earth_relief_05m", filter="m600", D=4)

# Cartesian distances for projected grid
G = grdfilter(grid, filter="g100", D=0)

filter or F

Sets the filter type and width. Required option.

Syntax: F="xwidth[/width2][+modifiers]" or filter="xwidth[/width2][+modifiers]"

Where x is the filter type code followed by the full diameter width. Append /width2 for a rectangular filter (requires D="p" or D=0).

Convolution Filters: - b: Boxcar - All weights are equal - c: Cosine Arch - Weights follow a cosine arch curve
- g: Gaussian - Weights given by Gaussian function (width is 6×sigma) - f: Custom - Weights from filter weight grid file (requires D=0) - o: Operator - Weights from filter weight grid file, sum to zero (requires D=0)

Non-Convolution Filters: - m: Median - Returns median value [default] - +qquantile: Select another quantile (0-1 range) [default is 0.5] - p: Mode (probability) - Returns modal value - +l: Return lowermost mode if multiple - +u: Return uppermost mode if multiple - h: Histogram Mode - Modal value as center of dominant peak - Append /binwidth to specify binning interval - +c: Center bins on multiples of binwidth - +l: Return lowermost mode if multiple - +u: Return uppermost mode if multiple - l: Lower - Return minimum of all values - L: Lower+ - Return minimum of all positive values only - u: Upper - Return maximum of all values - U: Upper- - Return maximum of all negative values only

Modifiers: - +h: High-pass filtering [default is low-pass] - +c: Center bins (for histogram mode) - +l: Return lowermost value (for mode filters) - +u: Return uppermost value (for mode filters) - +qquantile: Specify quantile for median filter (0-1)

Examples:

# Median filter, 600 km width
G = grdfilter("input.nc", F="m600", D=4)

# Gaussian filter, 100 km width
G = grdfilter(grid, filter="g100", distance=4)

# High-pass Gaussian filter
G = grdfilter(grid, F="g100+h", D=4)

# 25th percentile (lower quartile)
G = grdfilter(grid, filter="m600+q0.25", D=4)

# Rectangular boxcar filter
G = grdfilter(grid, F="b50/20", D=0)

# Histogram mode with 10-unit bins
G = grdfilter(grid, filter="h600/10", D=4)

Optional Arguments

outgrid or G or save

Output grid file name or let the function return a GMTgrid object.

Syntax: G="filename.nc" or outgrid="filename.nc"

Modifiers: - =ID: Write specific file format - +d: Divide data values by divisor - +n: Replace invalid with NaN
- +o: Offset data values (or +oa for automatic) - +s: Scale data values (or +sa for automatic)

Examples:

# Save to file
grdfilter("input.nc", filter="m600", D=4, G="filtered.nc")

# Return GMTgrid object (default)
G = grdfilter("input.nc", filter="m600", D=4)

inc or I or spacing

Set the output grid spacing.

Syntax: I=x_inc[/y_inc] or inc="x_inc[/y_inc]" or spacing=x_inc

Geographic coordinates units: - d: Arc degrees - m: Arc minutes - s: Arc seconds - e, f, k, M, n, u: Length units (converted to degrees)

Modifiers: - +e: Adjust max extent to fit increment exactly - +n: Specify number of nodes instead of increment

Examples:

# 0.5 degree spacing
G = grdfilter(grid, F="m600", D=4, I=0.5)

# Different x and y spacing
G = grdfilter(grid, filter="g100", D=4, inc="1/0.5")

# 30 arc minutes
G = grdfilter(grid, F="m600", D=4, I="30m")

# 100 nodes in x, 50 in y
G = grdfilter(grid, filter="g50", D=4, I="100+n/50+n")

nans or N

Determine how NaN-values in input grid affect filtered output.

Syntax: N="directive" or nans="directive"

Directives: - i: Ignore all NaNs in calculation [Default] - p: Force output to NaN if any NaN found in filter circle - r: Like i but preserve NaN if input node was NaN (co-registered grids only)

Examples:

# Ignore NaNs (default)
G = grdfilter(grid, F="m600", D=4, N="i")

# Propagate NaNs
G = grdfilter(grid, filter="g100", D=4, nans="p")

# Preserve input NaNs
G = grdfilter(grid, F="m600", D=4, N="r")
  • R or region or limits : – limits=(xmin, xmax, ymin, ymax) | limits=(BB=(xmin, xmax, ymin, ymax),) | limits=(LLUR=(xmin, xmax, ymin, ymax),units=“unit”) | …more
    Specify the region of interest. More at [limits](../common_opts/opt_R.html). For perspective view view, optionally add zmin,zmax. This option may be used to indicate the range used for the 3-D axes. You may ask for a larger w/e/s/n region to have more room between the image and the axes.

region or R

Specify the output region (sub-region of input).

Syntax: R=(xmin, xmax, ymin, ymax) or R="xmin/xmax/ymin/ymax" or region=...

Examples:

# Tuple format
G = grdfilter(grid, F="m600", D=4, R=(150, 250, 10, 40))

# String format  
G = grdfilter(grid, filter="g100", D=4, region="150/250/10/40")

# Extract Pacific region
G = grdfilter("@earth_relief_05m", F="m600", D=4, R=(150,250,10,40), I=0.5)

toggle or T

Toggle the node registration for output grid (gridline ↔︎ pixel).

Examples:

# Toggle registration
G = grdfilter(grid, F="m600", D=4, T=true)

# Equivalent using toggle keyword
G = grdfilter(grid, filter="g100", D=4, toggle=true)

registration or r

Set node registration explicitly.

Syntax: r="g" (gridline) or r="p" (pixel)

Examples:

# Force gridline registration
G = grdfilter(grid, F="m600", D=4, r="g")

# Force pixel registration
G = grdfilter(grid, filter="g100", D=4, registration="p")

verbose or V

Control verbosity level.

Syntax: V=true or V=level or verbose=level

Examples:

# Verbose output
G = grdfilter(grid, F="m600", D=4, V=true)

# Specific verbosity level
G = grdfilter(grid, filter="g100", D=4, verbose=:w)

colinfo or f

Specify data types of input/output columns (time or geographical data).

Examples:

# Time/geographic data specification
G = grdfilter(grid, F="m600", D=4, f="0x,1t")

cores or x

Limit number of cores used in multi-threaded algorithms.

Syntax: x=n where: - n > 0: Use n cores - n < 0: Use (all - n) cores - x=true: Use all available cores [default]

Examples:

# Use 4 cores
G = grdfilter(grid, F="m600", D=4, x=4)

# Use all but 2 cores
G = grdfilter(grid, filter="g100", D=4, cores=-2)

# Single-threaded
G = grdfilter(grid, F="m600", D=4, x=1)

Examples

Basic Filtering

using GMT

# Load Earth relief data
grid = gmtread("@earth_relief_05m", region=(140,260,0,50))

# Apply median filter with 600 km width (spherical distances)
G = grdfilter(grid, filter="m600", D=4, region=(150,250,10,40), inc=0.5)
61×201 GMTgrid{Float32, 2}:
 -5368.0  -5432.0  -5508.5  -5554.5  …  -3837.0  -3801.0  -3765.0  -3727.5
 -5635.0  -5682.5  -5701.5  -5710.0     -3833.5  -3800.0  -3761.0  -3714.5
 -5734.0  -5767.5  -5792.0  -5805.5     -3834.5  -3802.0  -3765.0  -3721.0
 -5791.0  -5817.0  -5837.0  -5858.0     -3853.0  -3807.5  -3767.0  -3725.0
 -5821.5  -5856.5  -5885.0  -5903.0     -3881.5  -3825.0  -3781.0  -3743.0
 -5850.0  -5885.0  -5912.5  -5939.0  …  -3889.0  -3840.5  -3799.5  -3769.5
 -5871.0  -5898.0  -5925.5  -5953.0     -3889.5  -3842.5  -3803.5  -3777.0
 -5883.5  -5904.5  -5933.5  -5953.5     -3873.0  -3834.5  -3805.0  -3781.5
 -5886.0  -5906.5  -5933.5  -5951.5     -3847.0  -3820.0  -3798.0  -3773.0
 -5860.0  -5885.0  -5918.5  -5940.5     -3799.0  -3780.0  -3766.0  -3747.0
 -5796.5  -5820.5  -5856.0  -5878.0  …  -3733.0  -3711.0  -3703.5  -3699.5
 -5716.0  -5738.0  -5755.0  -5778.5     -3639.0  -3624.0  -3628.0  -3639.0
 -5647.5  -5659.0  -5657.0  -5672.5     -3554.0  -3541.5  -3554.5  -3576.5
     ⋮                               ⋱                                 ⋮
 -5987.5  -5986.5  -5978.5  -5963.5      1520.0   1623.0   1699.0   1732.0
 -5982.5  -5982.5  -5973.5  -5952.5  …   1595.0   1662.5   1725.0   1784.5
 -5962.5  -5961.0  -5953.0  -5941.0      1651.0   1720.0   1781.0   1835.5
 -5910.0  -5914.5  -5917.5  -5910.5      1695.0   1754.0   1820.0   1875.5
 -5860.5  -5866.5  -5872.0  -5870.5      1726.5   1778.5   1843.0   1906.5
 -5812.5  -5820.5  -5829.5  -5834.5      1745.5   1795.0   1846.5   1914.0
 -5775.0  -5782.5  -5791.0  -5799.5  …   1772.5   1806.0   1850.0   1915.0
 -5734.5  -5744.5  -5753.0  -5764.0      1784.5   1809.0   1855.0   1925.0
 -5686.0  -5704.0  -5716.0  -5727.5      1795.0   1837.0   1890.5   1955.0
 -5631.0  -5655.5  -5672.5  -5682.5      1831.5   1869.0   1925.5   1993.5
 -5527.5  -5573.5  -5595.5  -5609.0      1861.0   1894.0   1957.0   2026.5
 -5444.0  -5471.5  -5507.0  -5537.5  …   1894.0   1933.5   1986.0   2048.0

High-Pass Filtering

# High-pass filter (residuals from low-pass)
G_residual = grdfilter("@earth_relief_05m", 
    F="m600+h", 
    D=4, 
    R=(150,250,10,40), 
    I=0.5)

Different Filter Types

# Gaussian smoothing
G_gauss = grdfilter(grid, filter="g100", distance=4)

# Boxcar (moving average)
G_box = grdfilter(grid, F="b200", D=4)

# Cosine arch
G_cos = grdfilter(grid, filter="c150", D=4)

# Maximum value in window
G_max = grdfilter(grid, F="u500", D=4)

# Minimum value in window
G_min = grdfilter(grid, filter="l500", D=4)

Quantile Filtering

# Lower quartile (25th percentile)
G_q25 = grdfilter(grid, F="m600+q0.25", D=4)

# Upper quartile (75th percentile)
G_q75 = grdfilter(grid, filter="m600+q0.75", D=4)

# 90th percentile
G_q90 = grdfilter(grid, F="m600+q0.9", D=4)

Custom Filter from Grid

# Create custom anisotropic Gaussian filter
# exp(-0.5*r^2) where r^2 = (2x^2 + y^2 - 2xy)/6
filter_grid = grdmath("-R-10/10/-10/10 -I1 X 2 POW 2 MUL Y 2 POW ADD X Y MUL 2 MUL SUB 6 DIV NEG 2 DIV EXP DUP SUM DIV")

# Apply custom filter
G = grdfilter("ripples.nc", F="f$(filter_grid)", D=0, G="smooth.nc")

Rectangular Filters

# Rectangular boxcar filter (100 km × 50 km)
G = grdfilter(grid, F="b100/50", D=0, I=1)

# Rectangular Gaussian
G = grdfilter(grid, filter="g200/100", D=0)

Variable Filter Width

# Create variable width grid (must match output dimensions)
width_grid = # ... GMTgrid with varying filter widths

# Apply variable width median filter
G = grdfilter(grid, F=width_grid, D=4, I=0.5)

Sub-Region Extraction with Filtering

# Filter and extract Pacific region
G_pacific = grdfilter("@earth_relief_05m",
    filter="m600",
    distance=4,
    region=(150, 250, 10, 40),
    inc=0.5)

NaN Handling

# Ignore NaNs (default)
G1 = grdfilter(grid, F="m600", D=4, N="i")

# Propagate NaNs (conservative)
G2 = grdfilter(grid, F="m600", D=4, nans="p")

# Preserve input NaNs only
G3 = grdfilter(grid, F="m600", D=4, N="r")

Multi-Threaded Processing

# Use all available cores (default)
G = grdfilter(grid, F="m600", D=4)

# Use 8 cores
G = grdfilter(grid, filter="g100", D=4, x=8)

# Single-threaded
G = grdfilter(grid, F="m600", D=4, cores=1)

Comparison of Filter Types

using GMT

# Original data
grid = gmtread("@earth_relief_05m", region=(150,200,10,40))

# Apply different filters
G_median = grdfilter(grid, F="m600", D=4, I=0.5)
G_gauss = grdfilter(grid, F="g600", D=4, I=0.5)
G_boxcar = grdfilter(grid, F="b600", D=4, I=0.5)
G_mode = grdfilter(grid, F="p600", D=4, I=0.5)

# Visualize differences
subplot(grid=(2,2), F=(width=20, height=15))
    grdimage(G_median, panel=(1,1), title="Median")
    grdimage(G_gauss, panel=(1,2), title="Gaussian")
    grdimage(G_boxcar, panel=(2,1), title="Boxcar")
    grdimage(G_mode, panel=(2,2), title="Mode")
subplot(:show)

Distance Flag Details

The distance flag (D) determines how grid coordinates relate to filter width:

Flag Grid Units Width Units X Scaling Distance Calc Speed
p Pixels Pixels (odd) None Cartesian Fast
0 Any Same as grid None Cartesian Fast
1 Degrees Kilometers None Cartesian Fast
2 Degrees Kilometers cos(mid_y) Cartesian Fast
3 Degrees Kilometers cos(y) Cartesian Slow
4 Degrees Kilometers cos(y) Spherical Slow
5 Mercator Kilometers cos(y) Spherical Slow

Recommendations: - Use D=4 for accurate geographic filtering (most common) - Use D=0 for Cartesian grids or custom filters - Use D=1 or D=2 for faster approximate geographic filtering - Use D=5 for Mercator projected grids from img2mercgrd

Filter Type Comparison

Filter Type Use Case Notes
Boxcar (b) Convolution Simple smoothing Equal weights, sharp cutoff
Cosine Arch (c) Convolution Smooth lowpass Gradual weight decay
Gaussian (g) Convolution Standard smoothing Width = 6σ, smooth decay
Median (m) Non-convolution Outlier removal Robust to spikes
Mode (p) Non-convolution Peak detection Finds most common value
Histogram Mode (h) Non-convolution Peak detection Like mode with binning
Min/Max (l/u) Non-convolution Envelope detection Extract extremes
Custom (f) Convolution Specialized User-defined weights
Operator (o) Convolution Derivatives Weights sum to zero

Limitations

  1. Mercator grids (D=5): Input must be created by img2mercgrd with origin at Equator
  2. Performance: If output increments are not integer multiples of input increments, filtering is considerably slower
  3. Custom filters (f/o): Require D=0 and odd dimensions; output spacing must match or be integer multiples of input
  4. Variable width: Filter width grid must match output grid dimensions and registration

Tips for Julia Users

  1. Input formats: GMT.jl accepts:

    • GMTgrid objects
    • File names as strings
    • Remote GMT datasets (e.g., "@earth_relief_05m")
  2. Output options:

    • Omit G to return GMTgrid object
    • Specify G="filename.nc" to save to file
    • Use save= as alias for G=
  3. Boolean options: Use true instead of empty strings:

    • T=true not T=""
    • V=true not V=""
  4. Keyword flexibility: Most options have multiple aliases:

    • filter= or F=
    • distance= or D=
    • region= or R=
    • inc= or I= or spacing=
  5. Piping workflows: GMTgrid objects can be piped through functions:

    result = gmtread("input.nc") |>
             g -> grdfilter(g, F="m600", D=4) |>
             g -> grdimage(g, proj=:Mercator, show=true)
  6. Performance: For large grids, consider:

    • Using coarser output increments
    • Smaller filter widths
    • Faster distance flags (D=0,1,2 vs D=4)
    • Limiting cores if memory constrained

Geographical and Time Coordinates

When the output grid type is netCDF, coordinates are labeled “longitude”, “latitude”, or “time” based on input attributes or f/R options. Time coordinates are stored as relative time since epoch (specified by TIME_UNIT and TIME_EPOCH).

See Also

GMT.jl related functions: - grdfft - Filtering in frequency domain - grdmath - Grid calculator operations - grdproject - Map projection transformations - grdsample - Resampling grids - grdgradient - Gradient calculations

References